#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

void initQueue (struct Queue *ref) {
	ref->dequeue = &fDequeue;
	ref->enqueue = &fEnqueue;
	ref->free_queue = &fFreeQueue;
	ref->peek = &fPeek;
}

void fEnqueue (void *data, struct Queue *ref) {
	struct node_t *q = (struct node_t*)
			malloc (sizeof (struct node_t));
	q->data = data;
	q->next = NULL;
	if (ref->queueHead == NULL) {
		ref->queueHead = q;
		ref->queueTail = q;
	} else {
		ref->queueTail->next = q;
		ref->queueTail = q;
	}
}

void* fDequeue (struct Queue *ref) {
	if (ref->queueHead != NULL) {
		void *data = ref->queueHead->data;
		struct node_t *temp = ref->queueHead;
		ref->queueHead = ref->queueHead->next;
		free (temp);
		return data;
	} else {
		fprintf (stderr, "End of queue\n");
		return NULL;
	}
}

void * fPeek (struct Queue *ref) {
	return ref->queueHead->data;
}

void fFreeQueue(struct Queue *ref) {
	while (ref->queueHead != NULL) {
		struct node_t *temp = ref->queueHead;
		ref->queueHead->next = ref->queueHead;
		free (temp);
	}
}
